package org.massdeception.content.servlets.authors;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.appengine.api.users.User;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

public abstract class AuthorServlet extends HttpServlet
{
    public abstract void process(HttpServletRequest request, HttpServletResponse response, User user)
        throws IOException, ServletException;

    public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        handleRequest(request, response);
    }

    public void doPost(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        handleRequest(request, response);
    }

    private void handleRequest(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
    {
        UserService userService = UserServiceFactory.getUserService();
        User user = userService.getCurrentUser();

        if (user != null)
        {
            this.process(request, response, user);
        } else
        {
            response.sendRedirect(userService.createLoginURL(request.getRequestURI()));
            return;
        }
    }
}
